//
//  LTLTabBarController.m
//  WeiBo
//
//  Created by Apple_Lzzy46 on 16/9/12.
//  Copyright © 2016年 LTL. All rights reserved.
//

#import "LTLTabBarController.h"
#import "LTLme.h"
#import "LTLfaXian.h"
#import "LTLshouYe.h"
#import "LTLxiaoXi.h"
#import "LTLTabBar.h"
//#import "LTLViewController.h"
#import "LTLComposeViewController.h"

@interface LTLTabBarController ()<LTLTabBarDelegate>

@property(nonatomic,strong)LTLshouYe *shouYe;
@property(nonatomic)NSInteger taberTag;
@end

@implementation LTLTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    ///自定义taber
   LTLTabBar *taber             = [[LTLTabBar alloc]init];

    taber.LTLdelegate               = self;
    ///使用KVC给系统taber
    [self setValue:taber forKey:@"tabBar"];

    ///添加控制器
    [self setUI];
}
/**
 * 添加控制器
 */
-(void)setUI
{
    self.shouYe = [LTLshouYe alloc];
    ///添加控制器
    [self didViewController:self.shouYe name:@"首页" image:@"tabbar_home" tag:1];
    [self didViewController:[LTLxiaoXi alloc] name:@"消息" image:@"tabbar_message_center" tag:2];
    [self didViewController:[LTLfaXian alloc] name:@"发现" image:@"tabbar_discover" tag:3];
    [self didViewController:[LTLme alloc] name:@"我" image:@"tabbar_profile" tag:4];

}

/**
 添加控制器方法

 @param controller 控制器
 @param name       控制器视图标题
 @param image      tabBarItem图片
 @param tag        tabBarItem的tag
 */
-(void)didViewController: (UITableViewController *)controller name:(NSString *)name image:(NSString *)image tag:(int)tag
{
    //    UIViewController *view = [[UIViewController alloc]init];
    UINavigationController *nav  = [[UINavigationController alloc]initWithRootViewController:controller];
    
    nav.navigationBar.translucent = YES;
    
//    nav.navigationBar.barTintColor = [UIColor blackColor];

    nav.tabBarItem.image         = [UIImage imageNamed:image];

    nav.tabBarItem.selectedImage = [UIImage imageNamed: [NSString stringWithFormat:@"%@_highlighted",image]];

    //    nav.tabBarItem.title = name;
    controller.title             = name;

    nav.tabBarItem.tag           = tag;

    [self addChildViewController:nav];
}

-(void)didAnNiuTabBar:(LTLTabBar *)tabber
{
    NSLog(@"20143309079");
    if ([LTLModel read].access_token) {
        
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[LTLComposeViewController alloc]init]];
//        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[LTLViewController alloc]init]];
        [self presentViewController:nav animated:YES completion:nil];
        
    }
    
}

///重写tabBer方法添加按钮动画
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag  == self.taberTag && item.tag == 1 ) {
        
        [self.shouYe.tableView.mj_header beginRefreshing];
    }
    self.taberTag = item.tag;
    
   __block NSInteger index      = 0;
    ///遍历tabBar.subviews 取出UITabBarButton
    [tabBar.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        if (idx == item.tag) {
            if ([obj isKindOfClass:NSClassFromString(@"UITabBarButton")])
            {
                ///遍历UITabBarButton subviews 取出UITabBarSwappableImageView
                [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

                        if ([obj isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {

                obj.transform   = CGAffineTransformMakeScale(0.8, 0.8);
                            ///动画
                            [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{

               obj.transform    = CGAffineTransformIdentity;

                            } completion:nil];
                        }

                }];

            }
        }
        index++;
    }];

}

@end
